<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Punish;
use App\Model\PunishDetail;

class PunishController extends Controller
{
    /*
    *获取处置处罚列表
    *@return array 处置处罚列表
    */
    public function list(Request $request)
    {
        $title = $request->input('title','');
        $where = array();
        if(!empty($title)) {
            $where[] = array('title', 'like', '%'.$title.'%');
        }
        $fields = array('id', 'title', 'updateTime');
        if(!empty($where)) {
            $list = Punish::where($where)->orderBy('updateTime', 'DESC')->select($fields)->get();
        } else {
            $list = Punish::orderBy('updateTime', 'DESC')->select($fields)->get();
        }
        $list = json_decode(json_encode($list), true);
        $returnList = $list;
        foreach($returnList as $k=>$v) {
            $returnList[$k]['updateTime'] = date('Y-m-d', $v['updateTime']);
        }

        return response()->json(array('data'=>$returnList, 'message'=>'返回成功' ,'code'=>200),200);
    }
    /*
    *处置处罚详情
    *@param id 处置处罚id
    */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        
        $punish = Punish::where('id', $id)->first();

        $punish_detail = PunishDetail::where('id', $id)->first();
        $punish_detail['content'] = html_entity_decode($punish_detail['content']);

        /*浏览量+1*/
        Punish::where('id', $id)->update(array('hits'=>$punish['hits']+1));

        return view('api.punish.detail', [
            'punish' => $punish,
            'punish_detail' => $punish_detail
        ]);
    }

}
